<?php

namespace app\api\logic;

use think\facade\Db;
use app\data\util\HttpUtil;
use think\exception\ValidateException;
use think\facade\Cache;

class ApiAssetLogic
{
    public function __construct()
    {

    }

    /**
     * 获取主题
     * @throws Throwable
     */
    public function ThemesList($params = [])
    {
        $shop_id        = $params['shop_id'] ?? 0;
        if(empty($shop_id)){
            return json(DefaultResponse(201, true, '暂无店铺', ''));
        }
        $shop_info      = Db::name('shops')->where('id',$shop_id)->find();
        $version        = Config('Logisticskey.shopify.api_versoin');
        $url            = $shop_info['myshopify_domain'].$version . "/themes.json";

        $return = HttpUtil::getUrl($url, $shop_info);
        
        $themes = $return['themes'];
        if (is_array($themes) && !empty($themes)) {
            //获取主题跟资产
            foreach ($themes as $theme) {
                $transfer_themes = Db::name('shopify_themes')->where('theme_id', $theme['id'])->find();
                if (empty($transfer_themes)) {
                    $insert = [
                        'shop_id' => $shop_info['id'] ?? "",
                        'theme_id' => $theme['id'] ?? "",
                        'name' => $theme['name'] ?? "",
                        'created_at' => $theme['created_at'] ?? "",
                        'updated_at' => $theme['updated_at'] ?? "",
                        'role' => $theme['role'] ?? "",
                        'theme_store_id' => $theme['theme_store_id'] ?? "",
                        'previewable' => $theme['previewable'] ?? "",
                        'processing' => $theme['processing'] ?? "",
                        'admin_graphql_api_id' => $theme['admin_graphql_api_id'] ?? "",
                        'createtime' => time(),
                        'updatetime' => time(),
                    ];
                    Db::name('shopify_themes')->insert($insert);
                }
            }
        }
        return json(DefaultResponse(201, true, '操作成功', ''));
        
    }

    
    /**
     * 获取资产
     * @throws Throwable
     */
    public function ThemesAssets($params = [])
    {
        $shop_id        = $params['shop_id'] ?? 0;
        if(empty($shop_id)){
            return json(DefaultResponse(201, true, '暂无店铺', ''));
        }
        $shopify_themes = Db::name('shopify_themes')
            ->where('role','main')
            ->where('shop_id',$shop_id)
            ->order('id desc')
            ->find();
        $theme_id = $shopify_themes['theme_id'] ?? 0;
        if(empty($theme_id)){
            return json(DefaultResponse(201, true, '暂无主题', ''));
        }
        $shop_info      = Db::name('shops')->where('id',$shop_id)->find();
        $version        = Config('Logisticskey.shopify.api_versoin');
        $url            = $shop_info['myshopify_domain'].$version . "/themes/".$theme_id."/assets.json";
        // halt($url);
        
        $return = HttpUtil::getUrl($url, $shop_info);
        // halt($return);
        $assets = $return['assets'];
        if (is_array($assets) && !empty($assets)) {
            //获取主题跟资产
            Db::name('shopify_themes_asset')
                ->where('theme_id', $theme_id)
                ->delete();
            $inserts = [];
            foreach ($assets as $asset) {
                $insert = [];
                $insert['theme_id'] = $theme_id ?? "";
                $insert['created_at'] = $asset['created_at'] ?? "";
                $insert['updated_at'] = $asset['updated_at'] ?? "";
                $insert['key'] = $asset['key'] ?? "";
                $insert['public_url'] = $asset['public_url'] ?? "";
                $inserts[] = $insert;
            }
            Db::name('shopify_themes_asset')->insertAll($inserts);
        }
        
        return json(DefaultResponse(201, true, '操作成功', $return));
        
    }

    /**
     * 把外部链接转成资产
     * @throws Throwable
     */
    public function LinkToAssetConversion($params = [])
    {
        $shop_id     = 17;
        $link        = $params['url'] ?? 0;
        $type        = $params['type'] ?? "images";
        if(empty($shop_id)){
            return json(DefaultResponse(201, true, '暂无店铺', ''));
        }
        if(empty($link)){
            return json(DefaultResponse(201, true, '暂无链接', ''));
        }
        //评论图片放在固定主题上
        $shopify_themes = Db::name('shopify_themes')
            // ->where('role','main')
            ->where('theme_id',177586798962)
            ->where('shop_id',$shop_id)
            ->order('id desc')
            ->find();
        $theme_id = $shopify_themes['theme_id'] ?? 0;
        if(empty($theme_id)){
            return json(DefaultResponse(201, true, '暂无主题', ''));
        }
        $shop_info      = Db::name('shops')->where('id',$shop_id)->find();
        $version        = Config('Logisticskey.shopify.api_versoin');
        $url            = $shop_info['myshopify_domain'].$version . "/themes/".$theme_id."/assets.json";
        // halt($url);

        //开始转移
        $milliseconds = (int)(microtime(true) * 1000);
        if($type == "images"){
            $asset = array();
            //毫秒时间戳
            $asset['key'] = "assets/link".$milliseconds.rand(0,999).".jpg";
            $asset['src'] = $link;
            $asset['theme_id'] = $theme_id;
            $post['asset'] = $asset;
        }else{
            return "请选择类型";
        }
        
        $return = HttpUtil::putUrl($url, $post, $shop_info);
        $return = json_decode($return, true);
        if (!empty($return['errors'])) {
            $url = $return['errors'] ?? "未知错误";
        }else{
            $url = $return['asset']['public_url'] ?? "";
        }
        return $url;
        
    }
}
